import { 
    createComment, 
    fetchComments,
    updateComment,
    deleteComment 
  } from '@/api/comment'
  
  const state = {
    comments: {}, // 格式: { postId: [comment1, comment2] }
    loading: false,
    error: null
  }
  
  const getters = {
    getCommentsByPostId: (state) => (postId) => {
      return state.comments[postId] || []
    }
  }
  
  const mutations = {
    SET_COMMENTS(state, { postId, comments }) {
      state.comments[postId] = comments
    },
    ADD_COMMENT(state, { postId, comment }) {
      if (!state.comments[postId]) {
        state.comments[postId] = []
      }
      state.comments[postId].unshift(comment)
    },
    UPDATE_COMMENT(state, { postId, commentId, content }) {
      if (state.comments[postId]) {
        const index = state.comments[postId].findIndex(c => c.id === commentId)
        if (index !== -1) {
          state.comments[postId][index].content = content
        }
      }
    },
    DELETE_COMMENT(state, { postId, commentId }) {
      if (state.comments[postId]) {
        state.comments[postId] = state.comments[postId].filter(c => c.id !== commentId)
      }
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_ERROR(state, error) {
      state.error = error
    }
  }
  
  const actions = {
    async fetchComments({ commit }, postId) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const comments = await fetchComments(postId)
        commit('SET_COMMENTS', { postId, comments })
        return comments
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async createComment({ commit }, { postId, content }) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        const comment = await createComment(postId, content)
        commit('ADD_COMMENT', { postId, comment })
        return comment
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async updateComment({ commit }, { postId, commentId, content }) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        await updateComment(commentId, content)
        commit('UPDATE_COMMENT', { postId, commentId, content })
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    async deleteComment({ commit }, { postId, commentId }) {
      commit('SET_LOADING', true)
      commit('SET_ERROR', null)
      
      try {
        await deleteComment(commentId)
        commit('DELETE_COMMENT', { postId, commentId })
      } catch (error) {
        commit('SET_ERROR', error.message)
        throw error
      } finally {
        commit('SET_LOADING', false)
      }
    }
  }
  
  export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
  }